package com.zb.test;

import com.zb.bean.Employee;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Lambda 表达式了；练习
 */
public class JAVA8_2_Lambda {
    /**
     *  需求：调用Collections.sorts()方法，通过定制排序，比较多个employee（先按年龄比，年龄相同按姓名比），使用Lambda作为参数传递
     *
     */

    List<Employee> emps = Arrays.asList(
            new Employee(1,"zhangbin",25),
            new Employee(2,"lisi",25),
            new Employee(3,"wangwu",40),
            new Employee(4,"zhaoliu",24));

    @Test
    public void test1(){
        Collections.sort(emps,(e1,e2)->{
            if(e1.getAge() == e2.getAge()){
                return e1.getName().compareTo(e2.getName());
            }else{
                return Integer.compare(e1.getAge(),e2.getAge());
            }
        });
        for (Employee emp : emps) {
            System.out.println(emp);
        }
    }

}
